response_callbacks = {};
unique_id = 10;

 # 环境设置
_env = {
    debug: _debug,
    env: "ios", #android java ios web
    pid: _pid,
    lang: _lang
}

FunctionSerialization = (jsFunction)-> 
    function_id = 'device_' + (unique_id++) + '_' + new Date().getTime()
    response_callbacks[function_id] = jsFunction
    function_id

touchEvent = (func, type)->
    return null if typeof func != "function"
    type = "onClick" if typeof type == "undefined"
    __context: _env.pid
    __type: type
    __function: FunctionSerialization func

log = if _debug then (TAG, message) ->
        TAG = "" if typeof TAG == "undefined"
        message = "" if typeof message == "undefined"
        kJavaScriptLog TAG, message
    else ()->

callCoreApi = (method, params, callback, action)->
    callback_id = FunctionSerialization callback
    event_id = null
    params = FunctionSerialization params if typeof params == "function"
    callDeviceApi method, params, callback_id, action

callJavaScriptFunction = ()->
    jsFuncId = arguments[arguments.length-1]
    jsFunc =  if response_callbacks[jsFuncId] then response_callbacks[jsFuncId] else eval ""+jsFuncId
    if typeof jsFunc == "function" then jsFunc.apply this, arguments else null

@callCoreApi = callCoreApi;
@_env = _env;
@_response_callbacks = response_callbacks;
@touchEvent = touchEvent

@callJavaScriptFunction = callJavaScriptFunction
@FunctionSerialization = FunctionSerialization

@console = {
    log: log
}